package us.versus.them.archetype;

import neko.FileSystem;

import neko.io.File;
import neko.io.FileInput;
import neko.io.Path;
import neko.io.Process;

import neko.zip.Reader;

class Extract {

	// this is a terrible solution...
	static var PARTIAL_PATH = '/archetype/1,0,1/archives';

	public static function base_directory() {
		var args = new Array<String>();
		args.push( "config" );
		var process = new Process( "haxelib", args );
		var path = process.stdout.readLine() + PARTIAL_PATH;
		process.close();

		path = ~/\/+/g.replace( path, '/' );
		return path;
	}

	public static function list() {
		File.stdout().writeString( 'types installed:\n' );

		for ( zip in neko.FileSystem.readDirectory( base_directory() ) ) {
			if ( ~/\.zip$/.match( zip ) ) {
				File.stdout().writeString( 
					' * ' +
					Path.withoutExtension( Path.withoutDirectory( zip ) ) + '\n' 
				);
			}
		}
	}

	public static function extract( type:String, values:Hash< String > ) {
		File.stdout().writeString( 'the type is "' + type + '"\n' );
		extractFilename( 
			Util.dozer( base_directory() + '/' + type + '.zip' )
			, values
		);
	}

	public static function extractFilename( path:String, values:Hash< String > ) {
		File.stdout().writeString( 'use ' + path + '\n' );
		extractFile( File.read( path, true ), values );
	}

	public static function extractFile( file:FileInput, values:Hash< String > ) {
		extractZipEntries( Reader.readZip( file ), values );
	}

	public static function extractZipEntries( zipEntries:List<ZipEntry>, values:Hash< String > ) {
		var readme : String = null;
		for ( zipEntry in zipEntries ) {
			var tmp = extractZipEntry( zipEntry, values );
			if ( null != tmp ) {
				readme = tmp;
			}
		}
		if ( null != readme ) {
			File.stdout().writeString( '----\n' + readme + '\n' );
		}
	}

	public static function extractZipEntry( zipEntry:ZipEntry, values:Hash< String > ) {
		var readme : String = null;

		var artifactId  = values.get( Archetype.ARTIFACT_ID_KEY  );
		var packageName = values.get( Archetype.PACKAGE_NAME_KEY );
		var version     = values.get( Archetype.VERSION_KEY  );

		var pwd = values.get( Util.PWD_KEY ); 

		var pdir = StringTools.replace( packageName, '.', '/' );

		var fileName = zipEntry.fileName;
		fileName = StringTools.replace( fileName, "_PACKAGE_NAME_", pdir );
		fileName = StringTools.replace( fileName, "_ARTIFACT_ID_", artifactId );
		fileName = StringTools.replace( fileName, "_VERSION_", version );

		fileName = pwd + fileName;

		if ( '/' == fileName.charAt( fileName.length - 1 ) ) {
			File.stdout().writeString( 'mkdir ' + fileName + '\n' );
			Util.mkdir( fileName );
		} else {

			var dirName = Util.dozer( Path.directory( fileName ) );
			if ( !FileSystem.exists( dirName ) ) {
				File.stdout().writeString( 'mkdir ' + dirName + '\n' );
				Util.mkdir( dirName );
			}

			fileName = Util.dozer( fileName );
			File.stdout().writeString( 'create file ' + fileName + '\n' );
			var file = File.write( fileName, false );

			var contents = Reader.unzip( zipEntry ).toString();
			contents = StringTools.replace( contents, ":PACKAGE_NAME:", packageName );
			contents = StringTools.replace( contents, ":ARTIFACT_ID:", artifactId );
			contents = StringTools.replace( contents, ":VERSION:", version );
			file.writeString( contents );
			file.close();

			if ( ~/readme/i.match( fileName ) ) {
				readme = contents;
			}
		}

		return readme;
	}
}
